package cn.kshost.zqyg.manager.service.impl;

import cn.kshost.zqyg.manager.mapper.ProductDetailsMapper;
import cn.kshost.zqyg.manager.mapper.ProductMapper;
import cn.kshost.zqyg.manager.mapper.ProductSkuMapper;
import cn.kshost.zqyg.manager.service.ProductService;
import cn.kshost.zqyg.model.dto.product.ProductDto;
import cn.kshost.zqyg.model.entity.product.Product;
import cn.kshost.zqyg.model.entity.product.ProductDetails;
import cn.kshost.zqyg.model.entity.product.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductSkuMapper productSkuMapper;
    @Autowired
    ProductDetailsMapper productDetailsMapper;
    @Override
    public PageInfo<Product> getProductList(int pageNum, int pageSize, ProductDto productDto) {
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
         List<Product> productList=  productMapper.selectProductList(productDto);
         return new PageInfo<>(productList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Product product) {
        //设置默认审核状态与上架状态
        product.setStatus(0);
        product.setAuditStatus(0);
        //插入商品
        productMapper.insert(product);
        //插入详情图片
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.insert(productDetails);
        //插入商品sku
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (int i = 0; i < productSkuList.size(); i++) {
            ProductSku productSku = productSkuList.get(i);
            //构建skuCode
            productSku.setSkuCode(product.getId()+"_"+i);
            //创建关联产品id
            productSku.setProductId(product.getId());
            //构建商品名称
            productSku.setSkuName(product.getName()+productSku.getSkuSpec());
            //设置销量
            productSku.setSaleNum(0);
            //设置状态
            productSku.setStatus(0);
            //插入数据
            productSkuMapper.insert(productSku);
        }
    }

    @Override
    public Product getById(long id) {
        //根据商品id查询商品数据
        Product product =  productMapper.selectById(id);
        //根据商品id查询sku数据
       List<ProductSku>  productSkuList =   productSkuMapper.selectByProductId(id);
        product.setProductSkuList(productSkuList);
        //根据商品id查询详情
        ProductDetails productDetails =  productDetailsMapper.selectByProductId(id);
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Product product) {
        //修改商品基本数据
        productMapper.updateById(product);
        //修改商品sku数据
        for (ProductSku productSku : product.getProductSkuList()) {
            productSkuMapper.updateById(productSku);
        }
        //修改商品详情数据
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.updateById(productDetails);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(long id) {

        //删除商品基本数据
        productMapper.deleteById(id);
        //删除商品sku数据
        productSkuMapper.deleteByProductId(id);
        //删除商品详情数据
        productDetailsMapper.deleteByProductId(id);

    }

    @Override
    public void updateAuditStatus(Long id, Long auditStatus) {
        Product product = new Product();
        product.setId(id);

        if (auditStatus ==1){
            product.setAuditStatus(1);
            product.setAuditMessage("审批成功");
        }else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审批失败");
        }
        productMapper.updateAuditStatus(product);
    }

    @Override
    public void updateStatus(Long id ,Long status) {
        Product product = new Product();
        product.setId(id);
        if (status ==1){
            product.setStatus(1);
        }else {
            product.setStatus(-1);
        }
        productMapper.updateStatus(product);
    }
}
